package com.kaily.timemanager.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaily.timemanager.R;
import com.kaily.timemanager.obj.TitleInfo;

/**
 * Created by zhoulikai on 17/2/26.
 */

public class TitleLayout extends LinearLayout implements View.OnClickListener {
    private TextView btnBack;
    private TextView titleText;
    private TextView btnAction;
    private TitleOnclickListener listener;

    public TitleLayout(Context context) {
        super(context);
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TitleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        btnBack = (TextView) this.findViewById(R.id.btn_back);
        titleText = (TextView) this.findViewById(R.id.title_tv);
        btnAction = (TextView) this.findViewById(R.id.btn_action);
        btnBack.setOnClickListener(this);
        btnAction.setOnClickListener(this);
    }

    public void setTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            return;
        }
        titleText.setText(title);
    }

    public void setBackRes(int resId) {
        if (resId > 0) {
            btnBack.setBackgroundResource(resId);
        }
    }

    public void setActionRes(int resId) {
        if (resId > 0) {
            btnAction.setBackgroundResource(resId);
        }
    }

    public void setListener(TitleOnclickListener listener) {
        this.listener = listener;
    }


    public void setTitleInfo(TitleInfo titleInfo) {
        if (titleInfo != null) {
            setTitle(titleInfo.title);
            setBackRes(titleInfo.backRes);
            setActionRes(titleInfo.actionRes);
            setListener(titleInfo.listener);

        }

    }

    public interface TitleOnclickListener {
        public void back(View view);

        public void action(View view);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                if (listener != null) {
                    listener.back(view);
                }
                break;
            case R.id.btn_action:
                if (listener != null) {
                    listener.action(view);
                }
                break;
        }

    }
}
